package org.Component;

import org.Panel.MainPanel;
import org.Tools.OnLineOP;
import org.example.Main;
import org.implement.Panel.PanelSize;
import org.implement.Panel.PanelSkip;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

public class SearchPanel extends JPanel {

    //搜索框
    JTextArea searchArea = new JTextArea();
    //搜索按钮
    JButton searchButton = new JButton("搜索");
    //滚动窗口
    JScrollPane searchScroll = new JScrollPane(searchArea);
    //母界面接口
    PanelSize panelSize;
    PanelSkip panelSkip;

    public SearchPanel(PanelSize panelSize, PanelSkip panelSkip) {
        this.panelSize = panelSize;
        this.panelSkip = panelSkip;

        setBounds(0,0,1000,100);
        setLayout(null);

        //字体
        Font fon = new Font(null,0,25);
        searchArea.setFont(fon);

        //搜索框
        searchScroll.setSize(600,60);
        searchScroll.setLocation(10,10);

        //搜索按钮
        searchButton.setSize(100,60);
        searchButton.setLocation(650,10);
        searchButton.addMouseListener(new MouseAdapter() {
            //用于判定鼠标是否在按钮内
            int f=1;
            @Override
            public void mouseClicked(MouseEvent e) {}

            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    //传给服务器端端数据
                    String msg = "search-"+searchArea.getText()+"-"+ MainPanel.userName;
                    //服务器端获取的数据
                    OnLineOP onLineOP = new OnLineOP();
                    String jsonmsg = null;
                    try {
                        jsonmsg = onLineOP.sendone(msg);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    MainPanel.AllPost = jsonmsg;
                    searchArea.setText("");
                    //跳转
                    panelSkip.skip(4);

                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });

        //将控件添加至界面
        add(searchScroll);
        add(searchButton);

    }
}
